package com.uncp.comisiondeadmision.dao;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import com.uncp.comisiondeadmision.util.entity.ECarrera;

public class DCarrera implements IDCarrera {
	
	
	private EntityManagerFactory emf;
	private EntityManager em;

	@SuppressWarnings("rawtypes")
	public List<SelectItem> listarCarreras(int codFacultad, int codEscuela) {
		
		em = emf.createEntityManager();
		List<SelectItem> lstCarreras = new ArrayList<SelectItem>();
		

		try {

			Query query = em
			.createQuery("select c from ECarrera c where c.int_codescuela = :codEscuela and c.int_codfacultad = :codFacultad");
			
			query.setParameter("codFacultad",codFacultad);
			query.setParameter("codEscuela",codEscuela);
			
			
			
			List carreras = query.getResultList();


			for (int i = 0; i < carreras.size(); i++) {

				ECarrera carrera =(ECarrera)carreras.get(i);
				
				SelectItem item = new SelectItem(carrera.getInt_codcarrera(),carrera.getStr_nombre());
				
				lstCarreras.add(item);

			}


		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			em.close();
		}
		
		return lstCarreras;
	}

	public void setEmf(EntityManagerFactory emf) {
		this.emf = emf;
	}
	
	

}
